
package com.winway.WinCRM.application.impl.Customer;

import java.util.List;
import java.util.ArrayList;
import java.text.MessageFormat;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.dayatang.querychannel.support.Page;
import com.dayatang.querychannel.service.QueryChannelService;
import com.winway.WinCRM.application.dto.*;
import com.winway.WinCRM.application.Customer.Cust_DepartmentApplication;
import com.winway.WinCRM.core.Customer.*;

@Named
@Transactional
public class Cust_DepartmentApplicationImpl implements Cust_DepartmentApplication {

	@Inject
	private QueryChannelService queryChannel;
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Cust_DepartmentDTO getCust_Department(Long id) {
		Cust_Department cust_Department = Cust_Department.load(Cust_Department.class, id);
		Cust_DepartmentDTO cust_DepartmentDTO = new Cust_DepartmentDTO();
		// 将domain转成VO
		try {
			BeanUtils.copyProperties(cust_DepartmentDTO, cust_Department);
		} catch (Exception e) {
			e.printStackTrace();
		}
		cust_DepartmentDTO.setId((java.lang.Long)cust_Department.getId());
		return cust_DepartmentDTO;
	}
	
	public Cust_DepartmentDTO saveCust_Department(Cust_DepartmentDTO cust_DepartmentDTO) {
		Cust_Department cust_Department = new Cust_Department();
		try {
        	BeanUtils.copyProperties(cust_Department, cust_DepartmentDTO);
        } catch (Exception e) {
        	e.printStackTrace();
        }
		cust_Department.save();
		cust_DepartmentDTO.setId((java.lang.Long)cust_Department.getId());
		return cust_DepartmentDTO;
	}
	
	public void updateCust_Department(Cust_DepartmentDTO cust_DepartmentDTO) {
		Cust_Department cust_Department = Cust_Department.get(Cust_Department.class, cust_DepartmentDTO.getId());
		// 设置要更新的值
		try {
			BeanUtils.copyProperties(cust_Department, cust_DepartmentDTO);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removeCust_Department(Long id) {
		this.removeCust_Departments(new Long[] { id });
	}
	
	public void removeCust_Departments(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			Cust_Department cust_Department = Cust_Department.load(Cust_Department.class, ids[i]);
			cust_Department.remove();
		}
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<Cust_DepartmentDTO> findAllCust_Department() {
		List<Cust_DepartmentDTO> list = new ArrayList<Cust_DepartmentDTO>();
		List<Cust_Department> all = Cust_Department.findAll(Cust_Department.class);
		for (Cust_Department cust_Department : all) {
			Cust_DepartmentDTO cust_DepartmentDTO = new Cust_DepartmentDTO();
			// 将domain转成VO
			try {
				BeanUtils.copyProperties(cust_DepartmentDTO, cust_Department);
			} catch (Exception e) {
				e.printStackTrace();
			}
			list.add(cust_DepartmentDTO);
		}
		return list;
	}
	
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<Cust_DepartmentDTO> pageQueryCust_Department(Cust_DepartmentDTO queryVo, int currentPage, int pageSize) {
		List<Cust_DepartmentDTO> result = new ArrayList<Cust_DepartmentDTO>();
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _cust_Department from Cust_Department _cust_Department   where 1=1 ");
	
	   	if (queryVo.getDepartment_Name() != null && !"".equals(queryVo.getDepartment_Name())) {
	   		jpql.append(" and _cust_Department.Department_Name like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDepartment_Name()));
	   	}		
	
	   	if (queryVo.getDepartment_ID() != null && !"".equals(queryVo.getDepartment_ID())) {
	   		jpql.append(" and _cust_Department.Department_ID like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDepartment_ID()));
	   	}		
	
	   	if (queryVo.getDepartment_Web() != null && !"".equals(queryVo.getDepartment_Web())) {
	   		jpql.append(" and _cust_Department.Department_Web like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDepartment_Web()));
	   	}		
	
	   	if (queryVo.getDepartment_Address() != null && !"".equals(queryVo.getDepartment_Address())) {
	   		jpql.append(" and _cust_Department.Department_Address like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDepartment_Address()));
	   	}		
	
	   	if (queryVo.getAdder() != null && !"".equals(queryVo.getAdder())) {
	   		jpql.append(" and _cust_Department.Adder like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getAdder()));
	   	}		
	
	   	if (queryVo.getAddTime() != null) {
	   		jpql.append(" and _cust_Department.AddTime between ? and ? ");
	   		conditionVals.add(queryVo.getAddTime());
	   		conditionVals.add(queryVo.getAddTimeEnd());
	   	}	
	
        Page<Cust_Department> pages = queryChannel.queryPagedResultByPageNo(jpql.toString(), conditionVals.toArray(), currentPage, pageSize);
        for (Cust_Department cust_Department : pages.getResult()) {
            Cust_DepartmentDTO cust_DepartmentDTO = new Cust_DepartmentDTO();
            
             // 将domain转成VO
            try {
            	BeanUtils.copyProperties(cust_DepartmentDTO, cust_Department);
            } catch (Exception e) {
            	e.printStackTrace();
            } 
            
                                                                                                            result.add(cust_DepartmentDTO);
        }
        return new Page<Cust_DepartmentDTO>(pages.getCurrentPageNo(), pages.getTotalCount(), pages.getPageSize(), result);
	}
	
	
}
